<?php
class DatabaseObject_Remark extends DatabaseObject {
	public function __construct($db) {
		parent::__construct($db, 'remarks', 'remarkid');
		$this->add('articleid', 0);
		$this->add('userid', 0);
		$this->add('username', 0);
		$this->add('dateline', 0);
		$this->add('content', '');
	}
	
    static public function _GetBaseQuery($db, $options) {
        $defaults = array('articleid' => array());
        
        foreach ($defaults as $k => $v) {
            $options[$k] = array_key_exists($k, $options) ? $options[$k] : $v;
        }
        
        $select = $db->select();
        $select->from(array('r' => 'remarks'), array());
        if (count($options['articleid']) > 0) {
            $select->where('r.articleid in (?)', $options['articleid']);
        }
        return $select;
    }
    
    static public function GetRemarkCount($db, $options) {
        $select = self::_GetBaseQuery($db, $options);
        $select->from(null, 'count(*)');
        
        return $db->fetchOne($select);
    }
    
    static public function GetRemarks($db, $options = array()) {
        $defaults = array('offset' => 0,
                          'limit'  => 0,
                          'order'  => 'r.dateline');
        
        foreach ($defaults as $k => $v) {
            $options[$k] = array_key_exists($k, $options) ? $options[$k] : $v;
        }
        
        $select = self::_GetBaseQuery($db, $options);
        $select->from(null, 'r.*');
        
        if ($options['limit'] > 0) {
            $select->limit($options['limit'], $option['offset']);
        }
        
        $select->order($options['order']);
        
        $data = $db->fetchAll($select);
        $remarks = self::BuildMultiple($db, __CLASS__, $data);
        $remarkids = array_keys($remarks);
        if (count($remarkids) == 0) {
            return array();
        }
        
        return $remarks;
    }
}
?>